package com.hupu.jrs.ui.thread.collect

import com.hupu.jrs.api.game.GameApi
import com.hupu.jrs.injector.PerActivity
import com.hupu.jrs.util.ToastUtil
import java.util.ArrayList
import javax.inject.Inject
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Action1
import rx.functions.Func1

/**
 * Created by sll on 2016/3/11.
 */
@PerActivity
class ThreadCollectPresenter @Inject
constructor(private val mGameApi: GameApi) : CollectThreadListContract.Presenter {

    private var mSpecialView: CollectThreadListContract.View? = null
    private var mSubscription: Subscription? = null
    private val threads = ArrayList<Thread>()
    private var page = 1
    private var hasNextPage = true

    override fun onThreadReceive() {
        mSpecialView!!.showLoading()
        loadCollectList(page)
    }

    private fun loadCollectList(page: Int) {
        this.page = page
        mSubscription = mGameApi.getCollectList(page).map(Func1<Any, List<Thread>> { result ->
            if (page == 1) {
                threads.clear()
            }
            if (result != null && result.result != null) {
                val data = result.result
                hasNextPage = data.nextDataExists === 1
                threads.addAll(data.data)
                return@Func1 threads
            }
            null
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(Action1<List<Thread>> { threads ->
            if (threads != null) {
                if (threads.isEmpty()) {
                    mSpecialView!!.onEmpty()
                } else {
                    mSpecialView!!.onLoadCompleted(hasNextPage)
                    mSpecialView!!.onRefreshCompleted()
                    mSpecialView!!.hideLoading()
                    mSpecialView!!.renderThreads(threads)
                }
            } else {
                loadThreadError()
            }
        }, Action1<Throwable> { loadThreadError() })
    }

    private fun loadThreadError() {
        if (threads.isEmpty()) {
            mSpecialView!!.onError("数据加载失败")
        } else {
            mSpecialView!!.hideLoading()
            mSpecialView!!.onLoadCompleted(true)
            mSpecialView!!.onRefreshCompleted()
            ToastUtil.showToast("数据加载失败")
        }
    }

    override fun onRefresh() {
        page = 1
        loadCollectList(page)
    }

    override fun onReload() {
        mSpecialView!!.showLoading()
        loadCollectList(page)
    }

    override fun onLoadMore() {
        if (!hasNextPage) {
            ToastUtil.showToast("没有更多了~")
            mSpecialView!!.onLoadCompleted(false)
            return
        }
        loadCollectList(++page)
    }

    override fun attachView(view: CollectThreadListContract.View) {
        mSpecialView = view
    }

    override fun detachView() {
        if (mSubscription != null && !mSubscription!!.isUnsubscribed) {
            mSubscription!!.unsubscribe()
        }
        mSpecialView = null
    }
}
